package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.MemberCash;
import com.dingreading.cloud.admin.entity.table.MemberCashTableDef;
import com.dingreading.cloud.admin.mapper.MemberCashMapper;
import com.dingreading.cloud.admin.service.MemberCashService;
import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.QueryFiledUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.dingreading.cloud.admin.entity.table.MembersTableDef.members;

/**
 *  服务层实现。
 *
 * @author Administrator
 * @since 2024-10-29
 */
@Service
public class MemberCashServiceImpl extends BaseServiceImpl<MemberCashMapper, MemberCash, MemberCashTableDef> implements MemberCashService {

    @Override
    protected MemberCashTableDef getTable() {
        return MemberCashTableDef.memberCash;
    }

    @Override
    public Page<MemberCash> pageList(PageUtil pageUtil, String corpUid, String storeUid, String keyword, String memberUid, Integer type, String beginDate, String endDate) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(corpUid))
            condition.and(table.corpUid.eq(corpUid));
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid));
        if (StringUtils.isNotBlank(memberUid))
            condition.and(table.memberUid.eq(memberUid));
        if (StringUtils.isNotBlank(keyword))
            condition.and(table.memberName.like(keyword)
                    .or(table.storeName.like(keyword))
            );
        if (type != null)
            condition.and(table.type.eq(type));
        if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate))
            condition.and(table.addTime.between(QueryFiledUtil.appendMin00(beginDate), QueryFiledUtil.appendMin24(endDate)));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(getColumns())
                .from(table)
                .leftJoin(members).on(table.memberUid.eq(members.uid))
                .where(condition)
                .orderBy(table.id.desc());

        return getMapper().paginate(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper);
    }

    @Override
    public List<MemberCash> listByCorpUid(List<String> corpUidList, String beginDate, String endDate) {
        QueryCondition condition = table.corpUid.in(corpUidList)
                .and(table.addTime.between(QueryFiledUtil.appendMin00(beginDate), QueryFiledUtil.appendMin24(endDate)));
        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.storeUid.asc(), table.addTime.asc());
        return getMapper().selectListByQuery(wrapper);
    }

    private QueryColumn[] getColumns() {
        return new QueryColumn[]{
                table.addTime, table.addStaffUid, table.addStaffName, table.editTime, table.lastStaffUid, table.lastStaffName,
                table.corpUid, table.storeUid, table.storeName, table.memberUid, table.memberName, table.type,
                table.typeName, table.money, table.uid, table.url, members.memberName.as("newMemberName")
        };
    }

}
